import * as api from '../service/api'
import fetch from '../util/fetch'


const state = {
	URL : {
		defaultImg : 'https://img.hidlq.com/image/shop/default.jpeg',
		shareBgImg : 'https://img.hidlq.com/image/shop/sharebg.png',
		logoImg : 'https://img.hidlq.com/image/shop/qmlogo.png',
		shop : 'https://shop.hidlq.com/#/',
		shop1 : 'https://shop.hidlq.com/#',
		api : 'https://api.hidlq.com/',
		ios : '',
	},
  	
}

const getters = {
	URL : state => state.URL,
}

const actions = {

	//公共的更新action，必须传fun, cb, err
	async update ({ }, { fun, cb, err, requestType, ...p }) {

		try{
			var type = requestType == 'get' ? 'GET' : 'POST';
			let res = await fetch(type, api[fun], p);
			if(res.errcode == 0){
				if(cb) cb(res.data);
			}else{
				if(err) err(res.errmsg);
			}
		}catch(error){
			console.log('update 中的异常')
			console.log(error);
		}
		
	},



	//公共的查询action，必须传fun, cb, err, st
	//约定  st 默认成 set + fun（fun的首字母大写）mutations
	async query ({ commit }, { fun, cb, err, st = 'set' + fun.substring(0,1).toUpperCase() + fun.substring(1), requestType, ...p }) {
		
		try{
			var type = requestType == 'get' ? 'GET' : 'POST';
			//let res = await api.go(p, api[fun], type);
			let res = await fetch(type, api[fun], p);
			if(res.errcode === 0){
				commit(st, res.data);
				if(cb) cb(res.data);
			}else{
				if(err) err(res.errmsg);
			}
		}catch(err){
			console.log(err);
		}
		
	},
}

const mutations = {
	setIOSURL (state, value) {
		if(state.URL.ios == ''){
			state.URL.ios = value;
		}
    },
}

export default {
  state,
  getters,
  actions,
  mutations
}
